package com.example.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.system.entity.Wheelchair;
import com.example.system.exception.CustomException;
import com.example.system.mapper.WheelchairMapper;
import com.example.system.service.IWheelchairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WheelchairServiceImpl extends ServiceImpl<WheelchairMapper, Wheelchair> implements IWheelchairService {
    @Autowired
    WheelchairMapper wheelchairMapper;

    public int deleteWheelchairByWheelchairId(Integer wheelchairId) {
        return wheelchairMapper.deleteWheelchairByWheelchairId(wheelchairId);
    }

    public void addWheelchairByWheelchair(Wheelchair wheelchair) {
        //类型一定要有不能为空
        if (wheelchair.getWheelchairType() == null || wheelchair.getWheelchairType() == 0) {
            throw new CustomException("类型不能为空");
        }
        //金额不能为负
        if (wheelchair.getWheelchairPrice() < 0) {
            throw new CustomException("金额不能为负数");
        }
        wheelchairMapper.addWheelchairByWheelchair(wheelchair);
    }


    public void updateWheelchairByWheelchair(Wheelchair wheelchair) {
        if (wheelchair == null) {
            throw new IllegalArgumentException("Wheelchair 对象不能为空");
        }
        Wheelchair usedWheelchair = wheelchairMapper.findWheelchairByWheelchairID(wheelchair.getWheelchairId());
        if (usedWheelchair == null) {
            throw new CustomException("未找到对应的 Wheelchair 条目");
        }
        if (!wheelchair.getUserId().equals(usedWheelchair.getUserId())) {
            throw new CustomException("当前请求修改的用户与原用户的用户名不同");
        }
        wheelchairMapper.updateWheelchairByWheelchair(wheelchair);
    }

    public void updateWheelchairFinishByWheelchairId(Integer wheelchairId) {
        if (wheelchairId == null) {
            throw new IllegalArgumentException("WheelchairId不能为空");
        }
        Wheelchair usedWheelchair = wheelchairMapper.findWheelchairByWheelchairID(wheelchairId);
        if (usedWheelchair == null) {
            throw new CustomException("未找到对应的 Wheelchair 条目");
        }
        wheelchairMapper.updateWheelchairFinishByWheelchairId(wheelchairId);
    }

    public Wheelchair findWheelchairByWheelchairId(Integer wheelchairId) {
        return wheelchairMapper.findWheelchairByWheelchairID(wheelchairId);
    }

    public List<Wheelchair> findWheelchairByUserID(Integer userId) {
        return wheelchairMapper.findWheelchairByUserId(userId);
    }


}
